
//
//  UIView+D_Geometry.m
//  EraserRubber_2_SaijT
//
//  Created by 丁诚昊 on 15/12/2.
//  Copyright © 2015年 ZSTech.iOS.丁诚昊(831). All rights reserved.
//

#import "UIView+D_Geometry.h"

/**
 * 快速从数组中随机筛选一个元素
 *  @return random element in the arrayList
 *  @param  array pointer,of protoType
 *  @author DimChumHual
 */
id(^pistonBlock)(NSArray* typeOCArray) = ^(NSArray* OCArray)
{
    u_int32_t idx = arc4random_uniform((u_int32_t)OCArray.count);
    id rs = OCArray[idx];
    return rs;
};

/**
 *  九宫格游戏
 *  @param count 传入的格子数量
 *  @return N宫格子的N的值
 *  @author DimChumHual
 */
int (^puzzleGame)(long) = ^(long count)
{
    int root = 1;
    for ( ; root <= sqrt(count) ; )
    {
        if ( root * root == count )
            break;
        else
            root++;
    }
    return root;
};

/** iOS大一统frames[]解决方案 :---> 基础方法定义与实现 */
static inline CGRect D_CGRectMake(CGFloat left , CGFloat top , CGFloat width, CGFloat height)
{
    NSLog(@"当前宽高比: .... ---> %.2f",DCHScreenSacleXOfiOS);
    CGRect rect;
    rect.origin.x = left * (screenSacleXOfiOS)DCHScreenSacleXOfiOS;
    rect.origin.y = top * (screenSacleXOfiOS)DCHScreenSacleXOfiOS;
    rect.size.width = width * (screenSacleXOfiOS)DCHScreenSacleXOfiOS;
    rect.size.height = height * (screenSacleXOfiOS)DCHScreenSacleXOfiOS;
    return rect;
}

@implementation UIView (QuartZ_Base)
@dynamic screenHeight;
@dynamic scrrenWidth;
-(void) foo
{
    D_CGRectMake(0, 0, 0, 0);
}

-(UIView*) RenderWithCGImage:(NSString*)backgroundIMG
{
    NSArray<NSString*>* subs = [backgroundIMG componentsSeparatedByString:@"."];
    NSLog(@"subs = %@",subs);
    
    if (subs.count >= 2)
    {
        UIImage *realImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
                                                               pathForResource:[subs firstObject]
                                                               ofType:[subs lastObject]]];
        self.layer.contents = (id)realImage.CGImage;
    }
    else
    {
        self.layer.contents = (id)[[UIImage imageNamed:backgroundIMG] CGImage];
    }
    
    return self;
}

@end

@implementation UIView (D_Geometry)
#pragma mark - Color & 色彩
+ (UIColor *)randomColor
{
    CGFloat hue = ( arc4random() % 420 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 640 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 640 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue
                      saturation:saturation
                      brightness:brightness
                           alpha:1];
}

#pragma mark -- Frame & Geopraphy 坐标系快捷方式设置
- (void)setD_Top:(CGFloat)D_Top{CGRect frame=self.frame;frame.origin.y= D_Top;self.frame = frame;}
- (void)setD_Left:(CGFloat)D_Left{CGRect frame = self.frame;frame.origin.x = D_Left;self.frame = frame;}
- (void)setD_CenterY:(CGFloat)D_CenterY{CGPoint oriCenter=self.center;oriCenter.y=D_CenterY;self.center=oriCenter;}
- (void)setD_CenterX:(CGFloat)D_CenterX{CGPoint oriCenter=self.center;oriCenter.x = D_CenterX;self.center=oriCenter;}
- (void)setD_Width:(CGFloat)D_Width{CGRect frame = self.frame;frame.size.width = D_Width;self.frame = frame;}
- (void)setD_Height:(CGFloat)D_Height{CGRect frame = self.frame;frame.size.height = D_Height;self.frame = frame;}
- (void)setD_Size:(CGSize)D_Size{CGRect frame = self.frame;frame.size = D_Size;self.frame = frame;}
-(CGFloat)D_Top{return self.frame.origin.y;}
-(CGFloat)D_Left{return self.frame.origin.x;}
-(CGFloat)D_CenterX{return self.center.x;}
-(CGFloat)D_CenterY{return self.center.y;}
-(CGFloat)D_Width{return self.frame.size.width;}
-(CGFloat)D_Height{return self.frame.size.height;}
-(CGSize)D_Size{return self.frame.size;}
@end
